/**
 * 路径分析
 * @author cqb
 * @class OpenLayers.Control.RouteAnalysis
 * @extends OpenLayers.Control
 * @version 1.0
 * @requires OpenLayers/Control.js
 */

OpenLayers.Control.RouteAnalysis = OpenLayers.Class(OpenLayers.Control,{
	/**
	 * Property: type {String} 工具类型
	 */
	type : OpenLayers.Control.TYPE_TOOL,

	/**
	 * Property: styleMap OpenLayers.styleMap
	 */
	styleMap : null,

	/**
	 * Property: title {String} 工具按钮提示文字
	 */
	title : "路径分析",
	/**
	 * 代理地址
	 * @property proxy
	 * @type {String}
	 */
	proxy: null,
	/**
	 * 路径分析地址
	 * @property server
	 * @type {String}
	 */
	server: null,
	/**
	 * 起始点图标
	 * @property startIcon
	 * @type {String}
	 */
	startIcon: null,
	/**
	 * 结束点图标
	 * @property endIcon
	 * @type {String}
	 */
	endIcon: null,
	/**
	 * 起始点坐标
	 * @property startPoint
	 * @type {LonLat}
	 */
	startPoint: null,
	/**
	 * 终点坐标
	 * @property endPoint
	 * @type {LonLat}
	 */
	endPoint: null,
	
	/**
	 * 绘制的样式
	 * @property routeSymbolizers
	 * @type {Object}
	 */
	routeSymbolizers: {
		"Line" : {
			strokeWidth : 3,
			strokeOpacity : 1,
			strokeColor : "#F0987C",
			strokeDashstyle : "solid"
		}
	},
	/**
	 * 起点handler
	 * @property fhandler
	 * @type {Handler}
	 */
	fhandler: null,
	/**
	 * 终点handler
	 * @property shandler
	 * @type {Handler}
	 */
	shandler: null,
	/**
	 * 图标图层
	 * @property markerlayer
	 * @type {Layer}
	 */
	markerlayer: null,
	/**
	 * 绘制矢量图层
	 * @property vectorlayer
	 * @type {Layer}
	 */
	vectorlayer: null,
	
	/**
	 * Method: initialize 初始化
	 */
	initialize : function(options) {
		OpenLayers.Control.prototype.initialize.apply(this,arguments);
		var style = new OpenLayers.Style();
		style.addRules([ new OpenLayers.Rule({
			symbolizer : this.routeSymbolizers
		}) ]);
		this.styleMap = new OpenLayers.StyleMap({
			"default" : style
		});
		
		this.startIcon = this.startIcon ? this.startIcon : OpenLayers.Util.getImageLocation("pin-orange.png");
		this.endIcon = this.endIcon ? this.endIcon : OpenLayers.Util.getImageLocation("pin-orange.png");
	},

	/**
	 * Method: activate 控件被触发时设置鼠标手势
	 */
	activate : function() {
		if(this.fhandler){
			this.fhandler.activate();
		}
		
		if(this.markerlayer){
			this.markerlayer.clearMarkers();
		}else{
			this.markerlayer = new OpenLayers.Layer.Markers("路径分析");
			this.map.addLayer(this.markerlayer);
		}
		if(this.vectorlayer){
			this.vectorlayer.removeAllFeatures();
		}else{
			this.vectorlayer = new OpenLayers.Layer.Vector("路径分析Vector",{
				styleMap: this.styleMap
			});
			this.map.addLayer(this.vectorlayer);
		}
		OpenLayers.Control.prototype.activate.apply(this,arguments);
		// this.map.layerContainerDiv.style.cursor = "url("
				// + AT21.WebGIS.getAT21RootLocation()
				// + "img/PointQuery.cur),default";
	},
	
	deactivate: function(){
		if(this.fhandler){
			this.fhandler.deactivate();
		}
		if(this.shandler){
			this.shandler.deactivate();
		}
		OpenLayers.Control.prototype.deactivate.apply(this,arguments);
		
		if(this.markerlayer){
			this.markerlayer.clearMarkers();
			this.map.removeLayer(this.markerlayer);
		}
		if(this.vectorlayer){
			this.vectorlayer.removeAllFeatures();
			this.map.removeLayer(this.vectorlayer);
		}
	},

	/**
	 * Method: draw
	 */
	draw : function() {
		this.fhandler = new OpenLayers.Handler.Click(this, {
			click : this.getFirstPoint
		});
		this.shandler = new OpenLayers.Handler.Click(this, {
			click : this.getSecondPoint
		});
	},

	/**
	 * 选取起始点并添加起始点图标并且激活终点handler
	 * @method getFirstPoint
	 * @param evt {OpenLayers.Event} 事件
	 */
	getFirstPoint : function(evt) {
		var lonlat = this.map.getLonLatFromViewPortPx(evt.xy);
		this.startPoint = lonlat;
		this.addPointMarker(lonlat, this.startIcon);
		this.fhandler.deactivate();
		if(this.shandler){
			this.shandler.activate();
		}
	},
	
	/**
	 * 选取终点
	 * @method getSecondPoint
	 * @param evt {OpenLayers.Event} 事件
	 */
	getSecondPoint : function(evt) {
		var lonlat = this.map.getLonLatFromViewPortPx(evt.xy);
		this.endPoint = lonlat;
		this.addPointMarker(lonlat, this.endIcon);
		this.shandler.deactivate();
		
		this.routeQuery();
	},
	
	/**
	 * 添加图标
	 * @method addPointMarker
	 * @param lonlat {LonLat} 坐标
	 * @param iconurl {String} 图标地址
	 */
	addPointMarker: function(lonlat, iconurl){
		var img = new Image();
		var scope = this;
		img.onload = function(){
			var size = new OpenLayers.Size(img.width, img.height);
			var offset = new OpenLayers.Pixel(0, -img.height);
			var icon = new OpenLayers.Icon(iconurl,size,offset);
			var marker = new OpenLayers.Marker(lonlat,icon);
			scope.markerlayer.addMarker(marker);
		}
		img.src = iconurl;
	},
	
	/**
	 * 开始查询 
	 * @method routeQuery
	 */
	routeQuery : function() {
		this.server = this.server.indexOf("?") == -1 ? this.server + "?" : this.server;
		var params = "&start="+this.startPoint+"&stop="+this.endPoint;
		var queryurl = this.server + params;
		var url = this.proxy ? this.proxy + "?url="+escape(queryurl) : queryurl;
		
		var scope = this;
		
		OpenLayers.Util.JSONP(url, function(result){
            if(typeof(result) == 'string'){
                result = eval("("+result+")");
            }
			var format = new OpenLayers.Format.RestJSON();
			result.routes.geometryType = "esriGeometryPolyline";
			features = format.read(result.routes);
			if(scope.vectorlayer){
				scope.vectorlayer.addFeatures(features);
			}
			scope.queryend(features);
		});
	},
	
	/**
	 * 查询结束
	 * @method queryend
	 * @param features {Array} 空间对象
	 */
	queryend: function(features){
		
	},
	
	CLASS_NAME : "OpenLayers.Control.RouteAnalysis"
});